﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using ECCSystem.BizRule;
namespace ECCSystem
{
	public partial class frmMessage : Form
	{
		DataSet dsTmp1;
		public frmMessage()
		{
			InitializeComponent();
		}
		public BackgroundWorker objBackgroundWorker = null;
		private void frmMessage_Load(object sender, EventArgs e)
		{
			if (objBackgroundWorker == null)
			{
				this.objBackgroundWorker = new BackgroundWorker();
			}
			ClsRule objClsRule = new ClsRule();
			dsTmp1 = (DataSet)objClsRule.GetEmailTempletList();
			//this.objBackgroundWorker = new BackgroundWorker();
			this.objBackgroundWorker.WorkerReportsProgress = true;
			this.objBackgroundWorker.WorkerSupportsCancellation = true;
			
			this.objBackgroundWorker.DoWork += MessageListener;
			this.objBackgroundWorker.ProgressChanged += RcvedMessage;
		}

		public void btnStart_Click(object sender, EventArgs e)
		{
			if (ClsCommon.gDataSet != null)
			{
				ClsCommon.gDataSet = null;
			}

			ClsCommon.gDataSet = new DataSet();
			ClsRule objClsRule = new ClsRule();
			ClsCommon.gDataSet = (DataSet)objClsRule.GetList();
			if (this.objBackgroundWorker.IsBusy == true)
			{
				this.objBackgroundWorker.CancelAsync();
				this.objBackgroundWorker.WorkerReportsProgress = false;
				this.objBackgroundWorker.WorkerSupportsCancellation = false;

				this.objBackgroundWorker.Dispose();
				this.objBackgroundWorker = null;
				this.objBackgroundWorker = new BackgroundWorker();
				this.objBackgroundWorker.WorkerReportsProgress = true;
				this.objBackgroundWorker.WorkerSupportsCancellation = true;

				this.objBackgroundWorker.DoWork += MessageListener;
				this.objBackgroundWorker.ProgressChanged += RcvedMessage;
			}
			if (ClsCommon.gDataSet != null)
			{
				if (ClsCommon.gDataSet.Tables.Count > 0 && ClsCommon.gDataSet.Tables[0].Rows.Count > 0)
				{
					this.objBackgroundWorker.RunWorkerAsync();
				}
			}			
		}

		private void MessageListener(object sender, DoWorkEventArgs e)
		{
			DateTime objStartTime = new DateTime();
			DateTime objEndTime = new DateTime();
			TimeSpan objTimeSpan = new TimeSpan();
			DataTable objDataTable = new DataTable();
			if (ClsCommon.gDataSet != null)
			{
				if (ClsCommon.gDataSet.Tables.Count > 0 && ClsCommon.gDataSet.Tables[0].Rows.Count > 0 && e.Cancel == false)
				{
					objDataTable = ClsCommon.gDataSet.Tables[0];
				}
			}
			int i = 0;
			
			objStartTime = DateTime.Now;

			DataSet dsTmp = new DataSet();
			dsTmp = dsTmp1.Clone();
			dsTmp = dsTmp1.Copy();
			DataView dvTmp = new DataView();
			dvTmp = dsTmp.Tables[0].DefaultView;
			dvTmp.RowFilter = "Rule_KeyID is not null or Rule_KeyID <> ''";
			Thread.Sleep(Convert.ToInt32(2000));
			do
			{
				if (objDataTable != null)
				{
					if (objDataTable.Rows.Count > 0 && e.Cancel == false)
					{

						DataView objDataView = new DataView();
						
						objDataView = objDataTable.DefaultView;

						for (int j = 0; j < dvTmp.Count; j++)
						{
							objDataView.RowFilter = "Rule_KeyID <> '" + dvTmp[j]["Rule_KeyID"].ToString() + "'";
							objDataView = objDataView.ToTable().DefaultView;
						}
						objDataView.RowFilter = "(Rule_TimeLimitedStartDate is null or Rule_TimeLimitedStartDate <= '" + DateTime.Now + "'"
											  + " and '" + DateTime.Now + "' <= Rule_TimeLimitedEndDate) and (Rule_Member = '' or Rule_Member like '%"
											  + ClsCommon.gStrOperatorName + "|%')";
						objDataView.Sort = "Rule_CreateDate DESC";

						while (i < objDataView.Count && e.Cancel == false)
						{
							objEndTime = DateTime.Now;
							objTimeSpan = objEndTime - objStartTime;
							string strMessage = "";
							//strMessage = strMessage + "规则标题:" + objDataView[i]["Rule_caption"].ToString() + " ";
							//if (objDataView[i]["Rule_TimeLimited"].ToString() == "True")
							//{
							//    strMessage = strMessage + "规则有效开始时间:" + objDataView[i]["Rule_TimeLimitedStartDate"].ToString() + " ";
							//    strMessage = strMessage + "规则有效结束时间:" + objDataView[i]["Rule_TimeLimitedEndDate"].ToString() + " ";
							//}
							strMessage = SetString(objDataView, i, strMessage, 3);
							i++;
							this.objBackgroundWorker.ReportProgress(0, strMessage);
							Thread.Sleep(Convert.ToInt32(ClsCommon.gIntTime));
							if (i == objDataTable.Rows.Count)
							{
								i = 0;
							}
							objEndTime = DateTime.Now;
						}

						Thread.Sleep(Convert.ToInt32(ClsCommon.gIntTime));
					}
					else if (objDataTable.Rows.Count == 0 && e.Cancel == false)
					{
						while (true && e.Cancel == false)
						{
							Thread.Sleep(Convert.ToInt32(ClsCommon.gIntTime));
						}
					}
				}

				Thread.Sleep(Convert.ToInt32(ClsCommon.gIntTime));

			} while (e.Cancel == false);

		}

		private void RcvedMessage(object sender, ProgressChangedEventArgs e)
		{
			this.Focus();
			this.txtMessage.Text = e.UserState.ToString();
			this.txtMessage.SelectAll();
			//this.objBackgroundWorker.ProgressChanged += null;
			//this.objBackgroundWorker.ProgressChanged += RcvedMessage;
		}

		private void frmMessage_FormClosed(object sender, FormClosedEventArgs e)
		{
			ClsCommon.gDataSet = null;
			ClsCommon.gfrmMessage = null;
			if (this.objBackgroundWorker != null)
			{
				this.objBackgroundWorker.ProgressChanged += null;
				this.objBackgroundWorker.CancelAsync();
				this.objBackgroundWorker.Dispose();
				this.objBackgroundWorker = null;
			}

			System.GC.Collect();

		}

		private void frmMessage_FormClosing(object sender, FormClosingEventArgs e)
		{
			//e.Cancel = true;
		}

		private string SetString(DataView objDataView,int i,string strMessage,int intTimes)
		{
			objDataView.Sort = "Rule_CreateDate DESC";
			if (objDataView.Count == 1)
			{
				strMessage = strMessage + "規則タイトル:" + objDataView[i]["Rule_caption"].ToString() + " ";
				if (objDataView[i]["Rule_TimeLimited"].ToString() == "True")
				{
					//strMessage = strMessage + "規則有効開始時間:" + objDataView[i]["Rule_TimeLimitedStartDate"].ToString() + " ";
					//strMessage = strMessage + "規則有効終了時間 :" + objDataView[i]["Rule_TimeLimitedEndDate"].ToString() + " ";
				}
				return strMessage + " ▓";
			}
			else if (objDataView.Count == 2)
			{
				strMessage = strMessage + "規則タイトル:" + objDataView[i]["Rule_caption"].ToString() + " ";
				if (objDataView[i]["Rule_TimeLimited"].ToString() == "True")
				{
					//strMessage = strMessage + "規則有効開始時間:" + objDataView[i]["Rule_TimeLimitedStartDate"].ToString() + " ";
					//strMessage = strMessage + "規則有効終了時間 :" + objDataView[i]["Rule_TimeLimitedEndDate"].ToString() + " ";
				}
				strMessage = strMessage + " ▓" + Environment.NewLine + Environment.NewLine;

				strMessage = strMessage + "規則タイトル:" + objDataView[1 - i]["Rule_caption"].ToString() + " ";
				if (objDataView[1 - i]["Rule_TimeLimited"].ToString() == "True")
				{
					//strMessage = strMessage + "規則有効開始時間:" + objDataView[1 - i]["Rule_TimeLimitedStartDate"].ToString() + " ";
					//strMessage = strMessage + "規則有効終了時間 :" + objDataView[1 - i]["Rule_TimeLimitedEndDate"].ToString() + " ";
				}
				return strMessage + " ▓";
			}
			else if (objDataView.Count == 0)
			{
				return "";
			}
			else
			{
				if (i == objDataView.Count)
				{
					i = 0;
				}
				if (intTimes != 0)
				{
					strMessage = strMessage + "規則タイトル:" + objDataView[i]["Rule_caption"].ToString() + " ";
					if (objDataView[i]["Rule_TimeLimited"].ToString() == "True")
					{
						//strMessage = strMessage + "規則有効開始時間:" + objDataView[i]["Rule_TimeLimitedStartDate"].ToString() + " ";
						//strMessage = strMessage + "規則有効終了時間 :" + objDataView[i]["Rule_TimeLimitedEndDate"].ToString() + " ";
					}
					strMessage = strMessage + " ▓" + Environment.NewLine + Environment.NewLine;
					intTimes = intTimes - 1;
					i++;
					strMessage = SetString(objDataView, i, strMessage, intTimes);
				}
				return strMessage + " ▓";

			}
		}

		private void btnMin_Click(object sender, EventArgs e)
		{
			this.WindowState = FormWindowState.Minimized;
		}

		private void txtMessage_DoubleClick(object sender, EventArgs e)
		{
			int intEndTextIndex = 0;
			if (txtMessage.SelectedText.Trim() != "▓")
			{
				return;
			}

			for (int i = txtMessage.SelectionStart; i >= 1 ; i--)
			{
				if (txtMessage.Text.Substring(i - 1,1) == "\n")
				{
					intEndTextIndex = i;
					break;
				}
				intEndTextIndex = i - 1;
			}

			string strSelect = txtMessage.Text.Substring(intEndTextIndex, txtMessage.SelectionStart - intEndTextIndex);
			strSelect = strSelect.Replace("規則タイトル:", "");

			frmRule objfrmRule = new frmRule(strSelect.Substring(0, strSelect.IndexOf(" ")));

			objfrmRule.ShowDialog();

		}

	}
}